﻿using System.ComponentModel;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Validation;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;

namespace Caraya.MFG.BusinessPlugIn
{
    [Description("【单据插件】执行操作的校验器"), HotUpdate]
    public class ExecuteOperationValidators : AbstractBillPlugIn
    {
        public override void BeforeDoOperation(BeforeDoOperationEventArgs e)
        {
            base.BeforeDoOperation(e);
            FormConst formConst = new FormConst();
            var bs = formConst.ToString();

            if (e.Operation.FormOperation.OperationId == 52)
            //if (e.Operation.FormOperation.Operation == "Push")
            {
                //仅针对下推操作执行校验器 
                var result = BusinessDataServiceHelper.Validate(this.View.Context,
                    this.View.BusinessInfo,
                    new[] { this.Model.DataObject },
                    e.Operation.FormOperation.Operation,
                    Enum_TimingPoint.Other,
                    e.Option);
                if (!result.IsSuccess)
                {
                    e.Cancel = true; GetFormatValidationInfo(result);
                    this.View.ShowOperateResult(result.OperateResult);
                }
                return;
            }
        }
        private void GetFormatValidationInfo(IOperationResult result)
        {
            var collection = result.OperateResult;
            foreach (var errorInfo in result.ValidationErrors)
            {
                var rs = new OperateResult
                {
                    PKValue = errorInfo.BillPKID,
                    RowIndex = errorInfo.RowIndex,
                    Name = errorInfo.Title,
                    SuccessStatus = false,
                    Message = errorInfo.Message,
                    MessageType = errorInfo.Level == ErrorLevel.Warning ? MessageType.Warning : MessageType.FatalError
                };
                if (!collection.Contains(rs))
                {
                    collection.Add(rs);
                }
            }


        }
    }
}
